package br.ufc.si.managedBean;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.context.FacesContext;
import javax.faces.context.Flash;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.PersistenceException;
import javax.servlet.http.HttpSession;

import dao.UsuarioDao;
import modelo.Usuario;


@ManagedBean(name="cadastroUsuarioBean")
public class CadastroUsuarioBean {
	
	Usuario usuario = new Usuario();
	String senha2;

	public String cadastrar() throws ClassNotFoundException{
		UsuarioDao dao = new UsuarioDao();
		
		Flash flash = FacesContext.getCurrentInstance().getExternalContext().getFlash();
		flash.setKeepMessages(true);
		
		try {
			dao.save(usuario);

			usuario = new Usuario();
			
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO,"Cadastrado com sucesso", ""));    
			
			return "cadastrar.xhtml?faces-redirect=true";
		} catch (PersistenceException e) {
			
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,"Email j� cadastrado", ""));
			
			return "cadastrar.xhtml?faces-redirect=true";
		}
		
	}

	public String atualizar() throws ClassNotFoundException{
		UsuarioDao dao = new UsuarioDao();
		Flash flash = FacesContext.getCurrentInstance().getExternalContext().getFlash();
		flash.setKeepMessages(true);
		
		try {
			//fun��o para fazer o update do usu�rio
			dao.update(usuario);
			
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO,"Usu�rio editado com sucesso", ""));
			
			return "visualizarusuario";
		} catch (Exception e) {
			
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,"Erro com o banco de dados", ""));
			
			return "inquilino/indexUsuario.xhtml?faces-redirect=true";
		}
		
	}

	public String editar() throws ClassNotFoundException{

		if(buscarUsuarioEmail()) {		
			return "/inquilino/editarusuario";
		} else {	
			Flash flash = FacesContext.getCurrentInstance().getExternalContext().getFlash();
			flash.setKeepMessages(true);
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,"Erro ao buscar usu�rio", ""));

			return "/inquilino/editarusuario?faces-redirect=true";
		}
	}

	public String visualizar() throws ClassNotFoundException{
		
		if(buscarUsuarioEmail()){	
			return "/inquilino/visualizarusuario";	
		} else{		
			Flash flash = FacesContext.getCurrentInstance().getExternalContext().getFlash();
			flash.setKeepMessages(true);
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,"Erro ao buscar usu�rio", ""));
			return "/inquilino/visualizarusuario?faces-redirect=true";
		}
		
	}
	
	private boolean buscarUsuarioEmail() throws ClassNotFoundException {
		UsuarioDao dao = new UsuarioDao();
		try {		
			FacesContext fc = FacesContext.getCurrentInstance();  
			HttpSession session = (HttpSession) fc.getExternalContext().getSession(true);  
			String email = (String) session.getAttribute("email");

			if(email != null){
				usuario = dao.find(email);
				return true;
			}else{
				return false;
			}
			
		} catch (PersistenceException e) {

			return false;
		}
	}
	
	public String cancelar() {
		return "/inquilino/indexUsuario";
	}

	public Usuario getUsuario() {
		return usuario;
	}

	public void setUsuario(Usuario usuario) {
		this.usuario = usuario;
	}

	public String getSenha2() {
		return senha2;
	}

	public void setSenha2(String senha2) {
		this.senha2 = senha2;
	}	
	
	
}
